from django.shortcuts import render

# Create your views here.
from dvadmin.utils.viewset import CustomModelViewSet
from quality_control.models import ProjectManageModel, DefectManageModel
from test_manage.models import CaseModuleModel
from quality_control.serializers import ProjectManageModelSerializer, DefectManageModelSerializer, ExportDefectSerializer
import django_filters
from dvadmin.utils.json_response import SuccessResponse


class ProjectManageModelViewSet(CustomModelViewSet):
    queryset = ProjectManageModel.objects.all()
    serializer_class = ProjectManageModelSerializer

    def destroy(self, request, *args, **kwargs):
        instance = self.get_object()
        if instance.id is not None:
            try:
                task = CaseModuleModel.objects.get(module_name=instance.project_name)
                task.delete()
            except:
                pass
        self.perform_destroy(instance)
        return SuccessResponse(data=[], msg="删除成功")

class DefectManageFilterSet(django_filters.FilterSet):
    accident_time = django_filters.BaseRangeFilter(field_name="accident_time")
    class Meta:
        model = DefectManageModel
        fields = '__all__'

class DefectManageModelViewSet(CustomModelViewSet):
    queryset = DefectManageModel.objects.all()
    serializer_class = DefectManageModelSerializer
    filter_class = DefectManageFilterSet

    export_field_label = {
        "defect_des": "缺陷描述",
        "defect_status": "缺陷状态",
        "defect_level": "缺陷等级",
        "defect_priority": "缺陷优先级",
        "accident_time": "缺陷产生时间",
        "resolution_time": "缺陷解决时间",
        "defect_type": "缺陷类型",
        "effect_range": "影响范围",
        "cause_occurrence": "产生原因",
        "improvement_action": "预防措施",
        "project_name": "所属项目",
        "module_name": "所属模块",
        "tester": "测试人员",
        "developer": "开发人员",
        "reproduce_step": "重现步骤",
    }
    export_serializer_class = ExportDefectSerializer